home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-05 | 2.5 KB | 107 lines | [MATS/MATL] |
- echo off;
- % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
- % To accompany the text:
- % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
- % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
- % This free software is complements of the author.
-
- % Algorithm 8.3 (Local Minimum Search: Quadratic Interpolation).
- % Section 8.1, Minimization of a Function, Page 416
- echo on; clc; format long; hold off; clear
-
- % This program finds the minimum of a function f(x).
-
- % The method is quadratic interpolation.
-
- % It is assumed that f(x) is unimodal over [a,b].
-
- % The function f(x) is to be placed in the M-file f.m
- % function z = f(x)
- % z = x.^2 - sin(x);
-
- delete f.m
- diary f.m; disp('function z = f(x)');...
- disp('z = x.^2 - sin(x);');...
- diary off;
-
- f(0); % Remark. f.m and quadmin.m are used for Algorithm 8.3
-
- pause % Press any key to continue.
-
- clc;
-
- % Place the endpoints interval [a,b] in a and b.
-
- a = 0;
-
- b = 1;
-
- pause % Press any key to graph the function.
-
- clc; clg;
- hs = (b-a)/150;
- Xs = a:hs:b;
- Ys = f(Xs); % User defined function.
- axis([-0.05 1.05 -0.25 0.16]);...
- plot(Xs,Ys,'g');...
- hold on;...
- plot([-0.05 1.05],[0 0],'b',[0 0],[-0.25 0.16],'b');...
- xlabel('x');...
- ylabel('y');...
- title('To search for the minimum of y = f(x).');...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- clc;
-
- % Place the endpoints interval [a,b] in a and b.
-
- % Place the tolerance for the abscissas in delta.
-
- % Place the tolerance for the ordinates in epsilon.
-
- a = 0;
-
- b = 1;
-
- delta = 1e-14;
-
- epsilon = 1E-15;
-
- [p,yp,dp,dy,P] = quadmin('f',a,b,delta,epsilon);
-
- pause % Press any key to find the minimum of f(x).
-
- clc; clg;
- hs = (b-a)/150;
- Xs = a:hs:b;
- Ys = f(Xs);
- YP = f(P);
- axis([a b -0.25 0.175]);...
- plot(Xs,Ys,'g',P,YP,'or');...
- hold on;...
- plot([a b],[0 0],'b',[0 0],[-0.25 0.175],'b');...
- xlabel('x');...
- ylabel('y');...
- title('Searching for the minimum of y = f(x).');...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- Mx1 = 'The results for a quadratic minimization search.';
- Mx2 = 'The solution is [p f(p)];';
- Mx3 = 'The abscissa p ';
- Mx4 = 'error bound for p';
- Mx5 = 'The minimum value f(p)';
- Mx6 = 'error bound for f(p)';
- Mx7 = 'The list of iterations is:';
- Mx8 = ' p(k) f(p(k))';
- clc,echo off,diary output,...
- disp(''),disp(Mx1),disp(''),disp(Mx2),disp(''),...
- disp(Mx3),disp(p),disp(Mx4),disp(['± ',num2str(abs(dp))]),disp(''),...
- disp(Mx5),disp(yp),disp(Mx4),disp(['± ',num2str(abs(dy))]),...
- disp(''),disp(Mx7),disp(Mx8),disp([P;YP]'),diary off,echo on
-